package com.fone.player.play;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import com.fone.player.util.L;

import de.greenrobot.event.EventBus;

/**
 * 播放器播放时的外部事件监听
 * 
 * @author kongxiaojun
 * @since 2014-4-16
 * 
 */
public class PlayerExternalEventReceiver {

	/** Log TAG */
	private static final String TAG = "PlayerExternalEventListener";
	/** sd卡广播接收器 */
	private BroadcastReceiver externalReceiver;

	public PlayerExternalEventReceiver(Context context) {
		super();
		initPhoneStateListener(context);
		initReceiver(context);
	}

	/**
	 * 销毁所有监听
	 */
	public void destory(Context context) {
		uninitReceiver(context);
	}

	/***
	 * 取消sd卡状态监听
	 * 
	 * @param context
	 */
	private void uninitReceiver(Context context) {
		try {
			if (null != externalReceiver) {
				context.unregisterReceiver(externalReceiver);
				externalReceiver = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 初始化SD卡状态监听
	 * 
	 * @param context
	 */
	private void initReceiver(Context context) {
		if (null == externalReceiver) {
			externalReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					if (Intent.ACTION_MEDIA_EJECT.equals(intent.getAction())) {
						EventBus.getDefault().post(new VideoPlayerMessage(VideoPlayerMessage.MESSGAE_SDCARD_EJECT));
					} else if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
						EventBus.getDefault().post(new VideoPlayerMessage(VideoPlayerMessage.MESSGAE_USER_PRESENT));
					} else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
						EventBus.getDefault().post(new VideoPlayerMessage(VideoPlayerMessage.MESSGAE_SCREEN_OFF));
					} else if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
						if (intent.hasExtra("state")) {
							if (intent.getIntExtra("state", -1) == 0) {
								// Toast.makeText(context,
								// "headset not connected",
								// Toast.LENGTH_LONG).show();
								L.i(TAG, "onReceive", "headset not connected");
								EventBus.getDefault().post(new VideoPlayerMessage(VideoPlayerMessage.MESSGAE_HEADSETPLUG_OUT));
							} else if (intent.getIntExtra("state", -1) == 1) {
								L.i(TAG, "onReceive", "headset connected");
							}
						}
					}
				}
			};

			IntentFilter intentFilter = new IntentFilter();
			intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
			intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
			intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
			intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
			intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
			intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
			// 解锁
			intentFilter.addAction(Intent.ACTION_USER_PRESENT);
			// 熄灭屏幕
			intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
			// 耳机
			intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);

			context.registerReceiver(externalReceiver, intentFilter);
		}
	}

	/**
	 * 初始化电话状态监听（比如来电话了，挂断电话）
	 * 
	 * @param context
	 */
	private void initPhoneStateListener(Context context) {
		TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		manager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
	}

	/**
	 * 电话状态监听
	 * 
	 * @author kongxiaojun
	 * @since 2014-4-16
	 * 
	 */
	private class MyPhoneStateListener extends PhoneStateListener {

		public void onCallStateChanged(int state, String incomingNumber) {
			switch (state) {
			case TelephonyManager.CALL_STATE_RINGING:
				EventBus.getDefault().post(new VideoPlayerMessage(VideoPlayerMessage.MESSGAE_CALL_STATE_RINGING));
				break;
			/**
			 * bug443 电话挂断后不继续播放，应该暂停
			 * */
			// case TelephonyManager.CALL_STATE_IDLE:
			// if (isPause && !playerAdapter.isAirOneMode()) {
			// playerAdapter.play();
			// isPause = false;
			// }
			// break;
			}
		}
	}

}
